from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from decimal import Decimal


# 定义一个分页器
class MyPage(PageNumberPagination):

    page_size = 10
    page_size_query_param = 'pagesize'
    page_query_param = 'page'
    max_page_size = 200

    def get_paginated_response(self, data):

        acitivty_info = self.request.data.get('acitivty_info', '')
        on__count = self.request.data.get('on__count', '')
        down__count = self.request.data.get('down__count', '')
        order_count = self.request.data.get('order_count', '')
        order_amount = self.request.data.get('order_amount', '')
        not_use_count = self.request.data.get('not_use_count', '')
        can_remain_number = self.request.data.get('can_remain_number', '')
        is_active_count = self.request.data.get('is_active_count', '')
        not_is_active_count = self.request.data.get('not_is_active_count', '')
        active_count = self.request.data.get('active_count', '')
        can_remain_amount = self.request.data.get('can_remain_amount', '')
        activity_time = self.request.data.get('activity_time', '')
        inviter_count = self.request.data.get('inviter_count', '')
        store_address = self.request.data.get('store_address', '')
        alidiscountcouponinfo = self.request.data.get('alidiscountcouponinfo', '')
        match_product = self.request.data.get('match_product', '')
        banner_short = self.request.data.get('banner_short', '')
        banner_long = self.request.data.get('banner_long', '')
        customer_send = self.request.data.get('customer_send', '')
        merchant_send = self.request.data.get('merchant_send', '')
        commented_count = self.request.data.get('commented_count', '')
        not_commented_count = self.request.data.get('not_commented_count', '')
        title_level = self.request.data.get('title_level', '')
        total_white_accout = self.request.data.get('total_white_accout', '')
        white_accout = self.request.data.get('white_accout', '')
        last_update_time = self.request.data.get('last_update_time', '')
        response_data = {
                'counts': self.page.paginator.count,
                'lists': data,
                'page': self.page.number,
                'pages': self.page.paginator.num_pages,
                'pagesize': self.page.paginator.per_page,
            }
        if total_white_accout:
            response_data['total_white_accout'] = total_white_accout
        if last_update_time:
            response_data['last_update_time'] = last_update_time
        if white_accout:
            response_data['white_accout'] = white_accout
        if title_level:
            response_data['title_level'] = title_level
        if not_commented_count:
            response_data['not_commented_count'] = not_commented_count
        if commented_count:
            response_data['commented_count'] = commented_count
        if acitivty_info:
            response_data['acitivty_info'] = acitivty_info
        if customer_send:
            response_data['customer_send'] = customer_send
        if merchant_send:
            response_data['merchant_send'] = merchant_send
        if on__count:
            response_data['on__count'] = on__count
        if down__count:
            response_data['down__count'] = down__count
        if order_count:
            response_data['order_count'] = order_count
        if order_amount:
            response_data['order_amount'] = order_amount
        if not_use_count:
            response_data['not_use_count'] = not_use_count
        if can_remain_number:
            response_data['can_remain_number'] = int(can_remain_number)
        if is_active_count:
            response_data['is_active_count'] = int(is_active_count)
        if not_is_active_count:
            response_data['not_is_active_count'] = int(not_is_active_count)
        if active_count:
            response_data['counts'] = int(active_count)
        if can_remain_amount:
            response_data['can_remain_amount'] = can_remain_amount
        if activity_time:
            response_data['activity_time'] = activity_time
        if banner_short:
            response_data['banner_short'] = banner_short
        if banner_long:
            response_data['banner_long'] = banner_long
        if inviter_count:
            response_data['inviter_count'] = inviter_count
        if store_address:
            response_data['store_address'] = store_address
        if match_product:
            response_data['match_product'] = match_product
        if alidiscountcouponinfo:
            if alidiscountcouponinfo == '无权限访问':
                lists = []
                for i in response_data['lists']:
                    i['preferential_price'] = Decimal(i['price'])
                    lists.append(i)
                response_data['lists'] = lists
                response_data['alidiscountcouponinfo'] = ''
            else:
                lists = []
                if alidiscountcouponinfo['coupon_type'] == 6:
                    for i in response_data['lists']:
                        i['preferential_price'] = Decimal('0')
                        lists.append(i)
                else:
                    for i in response_data['lists']:
                        i['preferential_price'] = Decimal(i['price']) - Decimal(alidiscountcouponinfo['amount'])
                        lists.append(i)
                response_data['lists'] = lists
                response_data['alidiscountcouponinfo'] = alidiscountcouponinfo
        return Response(response_data)


class BillPage(PageNumberPagination):

    page_size = 10
    page_size_query_param = 'pagesize'
    page_query_param = 'page'
    max_page_size = 100

    def get_paginated_response(self, data):

        date = self.request.query_params.get('date', None)
        data1 = data
        if data:
            if date:
                for i in data1:
                    if i['receipts_time'][:7] != date[:7]:
                        data.insert(data.index(i), {'receipts_time': i['receipts_time'][:7]})
                        date = i['receipts_time']
            else:
                data.insert(0, {'receipts_time': data[0]['receipts_time'][:7]})
                date = data[0]['receipts_time']
                for i in data1:
                    if i['receipts_time'][:7] != date[:7]:
                        data.insert(data.index(i), {'receipts_time': i['receipts_time'][:7]})
                        date = i['receipts_time']
        return Response({
            'counts': self.page.paginator.count,
            'lists': data,
            'page': self.page.number,
            'pages': self.page.paginator.num_pages,
            'pagesize': self.page.paginator.per_page,
        })


class ElectLikePage(PageNumberPagination):

    page_size = 10
    page_size_query_param = 'pagesize'
    page_query_param = 'page'
    max_page_size = 100

    def get_paginated_response(self, data):

        date = self.request.query_params.get('date', None)
        data1 = data
        if data:
            if date:
                for i in data1:
                    if i['create_time'][:10] != date[:10]:
                        data.insert(data.index(i), {'create_time': i['create_time'][:10]})
                        date = i['create_time']
            else:
                data.insert(0, {'create_time': data[0]['create_time'][:10]})
                date = data[0]['create_time']
                for i in data1:
                    if i['create_time'][:10] != date[:10]:
                        data.insert(data.index(i), {'create_time': i['create_time'][:10]})
                        date = i['create_time']
        return Response({
            'counts': self.page.paginator.count,
            'lists': data,
            'page': self.page.number,
            'pages': self.page.paginator.num_pages,
            'pagesize': self.page.paginator.per_page,
        })